package com.jshop.controller.admin;

import java.util.List;

import org.json.JSONException;
import com.jshop.model.Menu;
public class MenuController extends BaseController {
	public void list(){
		render("list.html");
	}
	public void treegrid(){
		List<Menu> list=Menu.dao.find("select * from menu order by menu_order asc");
		renderJson(list);
	}
	public void edit() throws JSONException{
		if(isPost()){
			boolean rs=Menu.dao.set("menu_name", getPara("menu_name"))
			.set("menu_id",getPara("menu_id"))
			.set("menu_url",getPara("menu_url"))
			.set("menu_parent_id",getParaToInt("menu_parent_id",0))
			.set("menu_icon", getPara("menu_icon"))
			.set("menu_order", getPara("menu_order")).update();
			if(rs){
				ajaxJsonSuccessMessage("修改成功");
			}else{
				ajaxJsonErrorMessage("修改失败");
			}
		}else{
			List<Menu> list=Menu.dao.find("select * from menu where menu_parent_id=0 order by menu_order asc");
			setAttr("list", list);
			setAttr("info", Menu.dao.findById(getParaToInt("id")));
			render("edit.html");
		}
	}
	public void add() throws JSONException{
		if(isPost()){
			Menu dao=new Menu();
			boolean rs=dao.set("menu_name", getPara("menu_name"))
			.set("menu_url",getPara("menu_url"))
			.set("menu_parent_id",getParaToInt("menu_parent_id",0))
			.set("menu_icon", getPara("menu_icon",""))
			.set("menu_order", getParaToInt("menu_order")).save();
			if(rs){
				ajaxJsonSuccessMessage("添加成功");
			}else{
				ajaxJsonErrorMessage("添加失败");
			}
		}else{
			List<Menu> list=Menu.dao.find("select * from menu where menu_parent_id=0 order by menu_order asc");
			setAttr("list", list);
			render("add.html");
		}
		
	}
	
	public void remove() throws JSONException{
		int id=getParaToInt("id");
		List<Menu> list=Menu.dao.find("select * from menu where menu_parent_id=?",id);
		if(list!=null&&list.size()>0){
			ajaxJsonErrorMessage("该菜单含有子菜单，不能删除");
		}else{
			boolean rs=Menu.dao.deleteById(id);
			if(rs){
				ajaxJsonSuccessMessage("删除成功");
			}else{
				ajaxJsonErrorMessage("删除失败");
			}
		}
	}
}
